package com.cly.controller;

import com.cly.anno.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "登出管理接口")
public class LogOutController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 登出操作
     * @return
     */
    @PostMapping("sys/logout")
    @ApiOperation("登出操作")
    @Log(operation = "登出操作")
    public ResponseEntity<String> logout(){
        //拿到当前jwt 从redis删掉就可以
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
        //这个token是没有bearer
        String tokenValue = details.getTokenValue();
        Boolean flag = redisTemplate.delete("oauth:jwt:" + tokenValue);
        System.out.println(flag);
        return ResponseEntity.ok("登出成功");
    }
}
